package com.zsh.web;

import com.zsh.pojo.Book;
import com.zsh.pojo.Page;
import com.zsh.service.BookService;
import com.zsh.service.impl.BookServiceImpl;
import com.zsh.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求参数，封装对象
        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());
        //2.调用service保存图书
        bookService.addBook(book);
        //3.跳转图书列表
        //request.getRequestDispatcher("/manager/"); //有bug，需要重定向
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),0);
        pageNo+=1;
        response.setContentType("text/html;charset=UTF-8");
        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        int id = WebUtils.parseInt(request.getParameter("id"),0);

        bookService.deleteBookById(id);

        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page");

    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Book book = WebUtils.copyParamToBean(request.getParameterMap(),new Book());

        bookService.updateBook(book);

        response.sendRedirect(request.getContextPath() + "/manager/bookServlet?action=page");
    }

    protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.获取参数
        int id = WebUtils.parseInt(request.getParameter("id"),0);
        //2.调用bookQuery
        Book book = bookService.queryBookById(id);
        //3.保存到request域
        request.setAttribute("book",book);
        //4.请求转发
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request,response);
    }
    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.通过bookService查询全部图书
        List<Book> books = bookService.queryBooks();
        for (Book book:books) {
            System.out.println(book);
        }
        //2.把全部图书保存再request域中
        request.setAttribute("books",books);
        //3.请求转发到jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);

    }

    /**
     * 处理分页
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1.获得请求参数，pageNo和pageSize
        int pageNo = WebUtils.parseInt(request.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        //2.调用bookService.page(pageNo,pageSize)
        Page<Book> page = bookService.page(pageNo,pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //3.保存PAge对象到requset
        request.setAttribute("page",page);
        //4.请求转发
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request,response);


    }
}
